﻿using System;

using Microsoft.Xna.Framework;

namespace SceneManager
{
    public abstract class SimpleObject
    {
        public Vector3 Position { get; set; }
        public Rotation Rotation { get; set; }

        public SimpleObject(Vector3 position)
        {
            Position = position;
            Rotation = new Rotation(0, 0, 0);
        }

        public bool Intersect(SimpleObject o)
        {
            if (this is Box)
            {
                if (o is Box)
                    return Intersect(this as Box, o as Box);
                else if (o is Sphere)
                    return Intersect(this as Box, o as Sphere);
            }
            else if (this is Sphere)
            {
                if (o is Box)
                    return Intersect(this as Box, o as Sphere);
                else if (o is Sphere)
                    return Intersect(this as Sphere, o as Sphere);
            }
            throw new NotImplementedException();
        }

        protected static bool Intersect(Box o1, Box o2)
        {
            return Math.Abs(o2.Position.X - o1.Position.X) < o1.Size.X / 2 + o2.Size.X / 2 &&
                   Math.Abs(o2.Position.Y - o1.Position.Y) < o1.Size.Y / 2 + o2.Size.Y / 2 &&
                   Math.Abs(o2.Position.Z - o1.Position.Z) < o1.Size.Z / 2 + o2.Size.Z / 2;
        }
        protected static bool Intersect(Box o1, Sphere o2)
        {
            return Math.Abs(o1.Position.X - o2.Position.X) < o1.Size.X / 2 + o2.Radius &&
                   Math.Abs(o1.Position.Y - o2.Position.Y) < o1.Size.Y / 2 + o2.Radius &&
                   Math.Abs(o1.Position.Z - o2.Position.Z) < o1.Size.X / 2 + o2.Radius;
        }
        protected static bool Intersect(Sphere o1, Sphere o2)
        {
            return Vector3.Distance(o1.Position, o2.Position) < o1.Radius + o2.Radius;
        }
    }
}
